/**
 * jims
 */
package com.jims.master.modules.reg.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.oper.entity.ClinicItemDict;
import com.jims.master.modules.oper.service.ClinicItemDictService;
import com.jims.master.modules.reg.entity.ClinicForRegist;
import com.jims.master.modules.reg.entity.ClinicIndex;
import com.jims.master.modules.reg.entity.ClinicSchedule;
import com.jims.master.modules.reg.service.ClinicForRegistService;
import com.jims.master.modules.reg.service.ClinicIndexService;
import com.jims.master.modules.reg.service.ClinicScheduleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 号别Controller
 *
 * @author chenxy
 * @version 2017-02-27
 */
@Controller
@RequestMapping(value = "${adminPath}/registration/clinicIndex")
public class ClinicIndexController extends BaseController {
    @Autowired
    private ClinicIndexService clinicIndexService;
    @Autowired
    private ClinicItemDictService clinicItemDictService;
    @Autowired
    private ClinicScheduleService clinicScheduleService;
    @Autowired
    private ClinicForRegistService clinicForRegistService;
    @ModelAttribute
    public ClinicIndex get(@RequestParam(required = false) String id) {
        ClinicIndex entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = clinicIndexService.get(id);
        }
        if (entity == null) {
            entity = new ClinicIndex();
        }
        return entity;
    }

    /**
     * 查询号别列表
     * @param clinicIndex
     * @param request
     * @param response
     * @param model
     * @return
     * @Author chenxy
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(ClinicIndex clinicIndex, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ClinicIndex> page = clinicIndexService.findPage(new Page<ClinicIndex>(request, response), clinicIndex);
        model.addAttribute("page", page);
        return "modules/reg/clinicIndexIndex";
    }
     /**
     *
     * @param clinicIndex
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(ClinicIndex clinicIndex, Model model) {
        model.addAttribute("clinicIndex", clinicIndex);
        return "modules/reg/clinicIndexForm";
    }

    /**
     * @param clinicIndex
     * @param model
     * @return
     * @version 1.0
     * @description 保存号别
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(ClinicIndex clinicIndex, Model model) {
        StringData data = new StringData();
        //根据号别名称生成输入码
        clinicIndex.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(clinicIndex.getName()));
        int i = clinicIndexService.save(clinicIndex);
        if (i > 0) {

            data.setCode("success");
            data.setData("保存号别成功");
        } else {
            data.setCode("error");
            data.setData("保存号别失败");
        }
        return data;
    }

    /**
     * @param clinicIndex
     * @return
     * @description <p>
     * 删除号别
     * 查询有没有外键约束情况
     * </p>
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(ClinicIndex clinicIndex) {
        StringData data = new StringData();
        List<ClinicSchedule> clinicScheduleList = this.clinicScheduleService.getByClinicIndexId(clinicIndex.getId());
        if (clinicScheduleList != null && !clinicScheduleList.isEmpty()) {
            data.setCode("error");
            data.setData("删除号别失败,存在出诊安排！");
            return data;
        }
        List<ClinicForRegist> clinicForRegists = this.clinicForRegistService.findbyClinicIndexId(clinicIndex.getId());
        if (clinicForRegists != null && !clinicForRegists.isEmpty()) {
            data.setCode("error");
            data.setData("删除号别失败,存在生成的号表！");
            return data;
        }
        int i = clinicIndexService.delete(clinicIndex);
        if (i > 0) {
            data.setCode("success");
            data.setData("删除号别成功");
        } else {
            data.setCode("error");
            data.setData("删除号别失败");
        }
        return data;
    }

}